export default {
  mounted() {
    window.addEventListener('message', this.handleMessage) // 子接收方式二参数
  },
  methods: {
    handleMessageFromParent(event) { // 子接收父参数
      console.log('父级发来消息了：', event)
    },
    showSuccessAlertAndCloseTab(remindText = '保存成功') {
      this.$commonUtils.showSuccessAlert(remindText, '关闭').then(_ => {
        window.parent.postMessage({
          cmd: 'closeTabItem',
          params: ''
        })
      })
    },
    sendCloseTabMessage(text, buttons) {
      if (undefined !== buttons && buttons.length === 1) {
        this.$alert(text, '提示', {
          confirmButtonText: buttons[0] === '' ? '确定' : buttons[0],
          callback: action => {
            window.parent.postMessage({
              cmd: 'closeTabItem',
              params: ''
            })
          }
        })
        return
      }
      this.$confirm(undefined === text ? '是否要关闭当前窗口?' : text, '提示', {
        confirmButtonText: undefined === buttons || buttons.length < 1 ? '关闭' : buttons[0],
        cancelButtonText: undefined === buttons || buttons.length < 2 ? '取消' : buttons[1],
        type: 'warning'
      }).then(() => {
        window.parent.postMessage({
          cmd: 'closeTabItem',
          params: ''
        })
        this.$EventBus.$emit('closeTabItem', '')
      }).catch(() => {
        location.reload()
      })
    }
  }
}
